//
// Created by Administrator on 2025/1/22/022.
//
#include "ui_lvgl_helper.h"
// group 焦点组
lv_group_t * ui_create_group_set_default(){
    lv_group_t * group = lv_group_create();
    lv_group_set_default(group);
    return group;
}
lv_group_t * ui_set_indev_group(lv_indev_t * indev){
    if(indev == NULL){
        return NULL;
    }
    lv_group_t * group = ui_create_group_set_default();
    lv_indev_set_group(indev, group);
    return group;
}

// screen
lv_obj_t * ui_create_screen(lv_obj_t *parent,lv_coord_t width,lv_coord_t height){
    lv_obj_t * screen = lv_obj_create(parent);
    lv_obj_clear_flag(screen, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    lv_obj_set_style_border_color(screen, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_width(screen, width);
    lv_obj_set_height(screen, height);
//    lv_obj_set_style_bg_color(screen, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT);
//    lv_obj_set_style_bg_opa(screen, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(screen, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(screen, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(screen, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(screen, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(screen, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(screen, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    return screen;
}
lv_obj_t * ui_create_default_screen(lv_obj_t *parent){
    return ui_create_screen(parent, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
}

// panel
lv_obj_t * ui_create_panel(lv_obj_t *parent,ui_panel_pro_t *pro){
    if(pro == NULL){
        return NULL;
    }
    lv_obj_t * panel = lv_obj_create(parent);
    lv_obj_set_width(panel, pro->width);
    lv_obj_set_height(panel, pro->height);

    if(pro->is_flex){
        lv_obj_set_flex_flow(panel, pro->flex_flow);
        lv_obj_set_flex_align(panel, pro->main_place, pro->cross_place, pro->track_place);
    }

    lv_obj_add_style(panel, pro->style, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLLABLE); /// Flags

    return panel;
}

lv_obj_t * ui_create_panel_base(lv_obj_t *parent,lv_coord_t width,lv_coord_t height,bool is_flex,bool is_column){
    ui_panel_pro_t pro;
    pro.width = width;
    pro.height = height;
    pro.is_flex = is_flex;
    if(is_flex){
        if(is_column){
            pro.flex_flow = LV_FLEX_FLOW_COLUMN;
        }else{
            pro.flex_flow = LV_FLEX_FLOW_ROW;
        }
        pro.main_place = LV_FLEX_ALIGN_CENTER;
        pro.cross_place = LV_FLEX_ALIGN_CENTER;
        pro.track_place = LV_FLEX_ALIGN_CENTER;
    }

    pro.style = ui_get_default_style();
    lv_obj_t * panel = ui_create_panel(parent,&pro);
    return panel;
}

lv_obj_t * ui_create_without_flex_panel(lv_obj_t *parent,lv_coord_t width,lv_coord_t height){
    return ui_create_panel_base(parent, width, height, false, false);
}

lv_obj_t * ui_create_default_without_flex_panel(lv_obj_t *parent){
    return ui_create_without_flex_panel(parent, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
}

lv_obj_t * ui_create_flex_panel(lv_obj_t *parent,lv_coord_t width,lv_coord_t height,bool is_column){
    return ui_create_panel_base(parent, width, height, true, is_column);
}

lv_obj_t * ui_create_column_panel(lv_obj_t *parent,lv_coord_t width,lv_coord_t height){
    return ui_create_flex_panel(parent, width, height, true);
}
lv_obj_t * ui_create_column_default_panel(lv_obj_t *parent){
    return ui_create_column_panel(parent, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
}
lv_obj_t * ui_create_row_panel(lv_obj_t *parent,lv_coord_t width,lv_coord_t height){
    return ui_create_flex_panel(parent,width,height,false);
}
lv_obj_t * ui_create_row_default_panel(lv_obj_t *parent){
    return ui_create_row_panel(parent, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
}

// image
lv_obj_t * ui_create_image_by_src(lv_obj_t *parent,ui_imager_pro_t *pro,const void * src){
    if(parent == NULL || pro == NULL){
        return NULL;
    }
    lv_obj_t * image = lv_img_create(parent);
    if(src){
        lv_img_set_src(image, src);
    }
    lv_obj_set_width(image, pro->width);
    lv_obj_set_height(image, pro->height);
    lv_obj_set_align(image, pro->align);
    lv_obj_add_flag(image, LV_OBJ_FLAG_ADV_HITTEST);  /// Flags
    lv_obj_clear_flag(image, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    return image;

}


lv_obj_t * ui_create_default_image_by_src(lv_obj_t *parent,const void * src){
    ui_imager_pro_t pro = {.height = LV_SIZE_CONTENT,.width = LV_SIZE_CONTENT,.align = LV_ALIGN_CENTER};
    return ui_create_image_by_src(parent, &pro, src);
}
lv_obj_t * ui_create_default_image(lv_obj_t *parent){
    ui_imager_pro_t pro = {.height = LV_SIZE_CONTENT,.width = LV_SIZE_CONTENT,.align = LV_ALIGN_CENTER};
    return ui_create_image_by_src(parent,&pro,NULL);
}

// label
lv_obj_t * ui_create_label(lv_obj_t *parent,ui_label_pro_t *pro,const char * text,lv_font_t * font){
    if(parent == NULL || pro == NULL){
        return NULL;
    }
    lv_obj_t * label = lv_label_create(parent);
    lv_obj_set_width(label, pro->width);
    lv_obj_set_height(label, pro->height);
    lv_obj_set_align(label, pro->align);

    lv_obj_set_style_text_color(label, pro->color_value, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(label, 255, LV_PART_MAIN | LV_STATE_DEFAULT);

    if(font){
        lv_obj_set_style_text_font(label, font, LV_PART_MAIN | LV_STATE_DEFAULT);
    }

    if(text){
        lv_label_set_text(label, text);
    }
    return label;

}
lv_obj_t * ui_create_default_label(lv_obj_t *parent,const char * text,lv_font_t * font){
    ui_label_pro_t pro = {.height = LV_SIZE_CONTENT,.width = LV_SIZE_CONTENT,.align = LV_ALIGN_CENTER,.color_value = lv_color_hex(0x000000)};
    return ui_create_label(parent, &pro, text, font);
}



// style
int ui_create_default_style(lv_style_t *style){
    if(style == NULL){
        return -1;
    }
    lv_style_init(style);

    lv_style_set_border_opa(style, 255);
    lv_style_set_border_width(style, 0);
    lv_style_set_border_color(style, lv_color_hex(0x000000));
    lv_style_set_border_side(style,LV_BORDER_SIDE_NONE);
    lv_style_set_radius(style, 0);

    lv_style_set_align(style, LV_ALIGN_CENTER);

//    lv_style_set_bg_opa(style, 0);

    lv_style_set_pad_all(style, 0);
    lv_style_set_pad_row(style, 0);
    lv_style_set_pad_column(style, 0);

}

lv_style_t * ui_get_default_style(){
    static lv_style_t style;
    static bool is_init = false;
    if(!is_init) {
        is_init = true;
        ui_create_default_style(&style);
    }
    return &style;
}


